# -*- coding: utf-8 -*-
'''
Created on 2013-4-11

@author: yq
'''
import sys,os,inspect
from django.conf.urls import patterns, url


suffix='Controller'
suffixC='Controller.pyc'
extensionName='.py'

def add(importObj):
    urlpatterns = patterns('',)
    
    '''
            迭代包下的文件
    '''
    files = os.listdir(importObj.__path__[0])
    for filepath in files:
     
        if str(filepath).find(suffix+extensionName)!=-1 & str(filepath).find(suffixC)==-1  :
            
            #模块名
            libname=str(filepath).replace(extensionName, "")
           
            #包+模块名
            classNames=importObj.__name__+"."+libname
           
            #*动态导入模块
            __import__(classNames)
            
            __forModules__(classNames,libname,urlpatterns)
            
    return urlpatterns

def __forModules__(classNames,libname,urlpatterns):
    '''
            迭代文件中的方法
    '''
    for func in inspect.getmembers(sys.modules[classNames],inspect.isfunction):
        tmp=libname.replace(suffix,"")
        urlpatterns+= patterns('',url("^"+tmp+"/"+func[0]+"/$", classNames+"."+func[0]),)
